var rootPath = getRootPath();
var flag = true;
$(function() {
	flag = true;
	refreshCheckCodePhone();
	
	var url = getUrlParam("url");
	console.log(url)
	
	$.ajax({
		url: rootPath + '/user/queryUserself',
        type: 'GET',
        contentType: 'application/json;charset=UTF-8',
        success: function (data) {
        	if(data.returnCode == '0000') {
        		var user = data.returnObject;
        		var phone = user.phone;
        		//手机号码后台已用*替换
        		if(phone != undefined && phone != '' && phone.length == 11) {
        			$("#contentInfo").css("display","block")
        			$("#phone").val(phone);
        			$("#checkcode").focus();
        		}else{
        			$("#contentInfo").css("display","none")
        			layer.alert("未绑定手机号，请先去绑定")
        		} 
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error");
        }
    });
	 
	//2.手机号码判断是否合法(移除焦点),如果合法,则可以获取验证码
	$("#checkcode").blur(function(){
		var checkCode = $("#checkcode").val();
		if(flag && checkCode && checkCode.length == 4){
			$("#sendCode").css({"background":"rgb(197,24,10)","cursor":"pointer"});
			$("#sendCode").attr("disabled",false);
		}else{
			$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
			$("#sendCode").attr("disabled",true);
		}
 
	});
	//3.两个框都有值的情况，确定按钮显示正常
	$("#checkcode,#code").bind('input propertychange',function() {
		if( $("#checkcode") == ''
			|| $.trim($("#checkcode").val()).length < 4
			|| $("#code").val() == ''
				|| $.trim($("#code").val()).length < 4) {
			$("#aopLogin").css({"background":"#bbbbbb","cursor":"default"});
			$("#aopLogin").attr("disabled",true);
			//click_flag = false;
		} else {
			$("#aopLogin").css({"background":"rgb(197,24,10)","cursor":"pointer"});
			$("#aopLogin").attr("disabled",false);
			//click_flag = true;
		}
	});
	//4.发送验证码之后，显示倒计时60秒。
	$("#sendCode").click(function() {
		event.stopPropagation();
		getCheckCode($(this));
	});		
	 
	//6.刷新验证码
	$("#mobileNextPhone,#nextPhone").click(refreshCheckCodePhone); 
	
	
	//登陆
	$("#aopLogin").click(function(){
		$.ajax({
			url: rootPath + '/userFront/aop/loginbymsg',
	        type: 'post',
	        contentType: 'application/json;charset=UTF-8',
	        data: JSON.stringify($("#aopLoginForm").serializeJson()),
	        success: function (data) {
	        	if(data.returnCode == '0000') {
	        		layer.msg('登陆成功，即将跳转...', {
						icon : 1,
						time : 1500
					},function(){
						if(url){
							window.location.href= url;
						}
					});
	        	}else {
	        		layer.msg(data.returnMessage, {
						icon : 2,
						time : 3000
					});
	        	}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error");
	        }
	    });
	});
});

//刷新验证码
function refreshCheckCodePhone() {
	$("#picCode").prop("src", '/account/captcha.jpg?tm=' + Math.random());
}

function getCheckCode(obj) {
	//不会产生 复制的原逻辑已被修改 但还是保留着~！！
	if($("#checkcode").val() == '' || $.trim($("#checkcode").val()).length < 4) {
		layer.alert("请输入图片验证码");
		return;
	}
	
	//倒计时中不能发送
	var countdown = $("#get_code").attr("countdown");
	if(countdown != '' && countdown>0){
		return;
	}
	setTime(obj);
	$.ajax({
		type: "get",
	    url: rootPath + "/userFront/sendCodeSmsValidate",
	    data: { 
	    	"flag" : 6,
	    	"checkCode" : $.trim($("#checkcode").val())
	    },
	    dataType: "json",
	    success: function (data) {
	    	if("0000" == data.returnCode) {
	    		layer.msg('已发送短信，请稍等', {
					icon : 1,
					time : 1500
				});
	        } else {
	        	refreshCheckCodePhone();
	        	layer.msg(data.returnMessage, {
					icon : 2,
					time : 1500
				});
		    }
	    },
	    error:function (jqXHR, textStatus, errorThrown) {
	    	refreshCheckCodePhone();
	    	layer.msg("发送验证码失败,请检查您的网络.", {
				icon : 2,
				time : 1500
			});
	    	
	    }
	});
}
//验证码倒计时
function setTime(obj) {
	var countdown = obj.attr("countdown");
	if(countdown == undefined || countdown == '' || countdown == 0) {
		countdown = 60;
	}
	var interval = setInterval(function() {
		countdown--;
		obj.attr("countdown",countdown);
		if(countdown == 0){
				flag = true;
				obj.css({"background":"rgb(197,24,10)","cursor":"pointer"});
				obj.attr('disabled',false); 
				obj.val("获取验证码");
		 
			window.clearInterval(interval);
		} else {
				flag = false;
				obj.css({"background-color":"#bbbbbb","cursor":"default"});
				obj.val("重新发送(" + countdown + ")");
				obj.attr('disabled',true);
			 
		}
	}, 1000);
}

function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return decodeURIComponent(r[2]);
	return null;
}